package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.News;
import com.mars.module.admin.entity.UserFeedback;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.UserFeedbackRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.UserFeedbackMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.UserFeedback;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IUserFeedbackService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户反馈业务层处理
 *
 * @author mars
 * @date 2024-03-04
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserFeedbackServiceImpl implements IUserFeedbackService {

    private final UserFeedbackMapper baseMapper;
    private final SysUserMapper userMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserFeedback add(UserFeedbackRequest request) {
        UserFeedback entity = UserFeedback.builder().build();
        BeanUtils.copyProperties(request, entity);
        entity.setCreateById(ContextUserInfoThreadHolder.get().getId());
        entity.setUserId(ContextUserInfoThreadHolder.get().getId());
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(UserFeedbackRequest request) {
        UserFeedback entity = UserFeedback.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public UserFeedback getById(Long id) {
        UserFeedback userFeedback = baseMapper.selectById(id);
        SysUser sysUser = userMapper.selectById(userFeedback.getUserId());
        userFeedback.setUserName(sysUser == null ? "" : sysUser.getUserName());
        return userFeedback;
    }

    @Override
    public PageInfo<UserFeedback> pageList(UserFeedbackRequest request) {
        Page<UserFeedback> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<UserFeedback> query = this.buildWrapper(request);
        IPage<UserFeedback> pageRecord = baseMapper.selectPage(page, query);
        List<UserFeedback> records = pageRecord.getRecords();
        List<UserFeedback> recordsNew = new ArrayList<>();
        for (UserFeedback collection : records) {
            SysUser sysUser = userMapper.selectById(collection.getUserId());
            collection.setUserName(sysUser == null ? "" : sysUser.getUserName());
            recordsNew.add(collection);
        }
        recordsNew = recordsNew.stream().sorted((m1, m2) -> m2.getCreateTime().compareTo(m1.getCreateTime())).collect(Collectors.toList());
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<UserFeedback> list(UserFeedbackRequest request) {
        LambdaQueryWrapper<UserFeedback> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<UserFeedback> buildWrapper(UserFeedbackRequest param) {
        LambdaQueryWrapper<UserFeedback> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getContent())) {
            query.like(UserFeedback::getContent, param.getContent());
        }
        if (StringUtils.isNotBlank(param.getCreateById())) {
            query.eq(UserFeedback::getCreateById, param.getCreateById());
        }
        return query;
    }

}
